package com.chat.consumer.core;

import com.chat.server.base.MetadataUnit;
import com.chat.server.base.PackageMeta;
import com.chat.server.base.ServiceRegister;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.util.List;

/**
 * @Auther created by haoteng on 2019/08/20
 */
public class ConsumerContext {

    public static final Multimap<String, MetadataUnit> registerMetadataMap = ArrayListMultimap.create();

    public static ServiceRegister serviceRegister = new ServiceRegister();


    /**
     * 根据服务名称获取注册信息（消费端初始化时调用）
     *
     * @param clazz
     */
    public void initRegisterMetadata(Class clazz) {
        List<MetadataUnit> metadataList = serviceRegister.getAllZkMetadata(clazz.getName());
        if (!metadataList.isEmpty()) {
            catchMetadataMap(clazz.getName(), metadataList);
        }
    }

    /**
     * 将服务注册信息缓存在本地（消费端初始化时调用）
     */
    private void catchMetadataMap(String interfacePath, List<MetadataUnit> metadataList) {
        synchronized (registerMetadataMap) {

            if (registerMetadataMap.get(interfacePath) != null) {
                registerMetadataMap.removeAll(interfacePath);
            }
            registerMetadataMap.putAll(interfacePath, metadataList);
        }
    }

}
